package example.controller;

import example.dao.connect;
import example.service.*;
import org.json.JSONException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

@Controller
@Resource
public class IndexController {
    @RequestMapping("/")
    public String index() {
        return "index";
    }

    @RequestMapping(value="/login_check",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public static String home_title(HttpServletRequest req)  {
    {
        return login_check.logsession(req);
    }
    }

    @RequestMapping(value="/login_shenfen",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public static String login_shenfen(HttpServletRequest req) throws SQLException, JSONException {
        {
            String username = req.getParameter("username");
            return login_check.logshenfen(username);
        }
    }

    @RequestMapping(value="/news_get",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public static String news_get() throws Exception {
        {
            return news_python.new_python();
        }
    }

    @RequestMapping(value="/get_dingdan",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public static String get_dingdan(HttpServletRequest req) throws SQLException, JSONException {
        {
            String username = req.getParameter("username");
            return dingdan_c.dingdan_f(username);
        }
    }

    @RequestMapping(value="/get_shoumai",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public static String get_shoumai(HttpServletRequest req) throws SQLException, JSONException {
        {
            String username = req.getParameter("username");
            return dingdan_c.dingdan_sale(username);
        }
    }

    @RequestMapping(value="/get_shanchu",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public static String get_shanchu(HttpServletRequest req) throws SQLException, JSONException {
        {
            String username = req.getParameter("username");
            return dingdan_c.dingdan_delect(username);
        }
    }

    @RequestMapping(value="/get_realding",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public static String get_realding(HttpServletRequest req) throws SQLException, JSONException {
        {
            String username = req.getParameter("username");
            return dingdan_c.dingdan_realding(username);
        }
    }

    @RequestMapping(value="/goumai",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public static String goumai(HttpServletRequest req) throws SQLException, JSONException {
        {
            String username = req.getParameter("username");
            String ID = req.getParameter("ID");
            String address = req.getParameter("address");
            dingdan_c.dingdan_buy( ID , address , username );
            return dingdan_c.dingdan_f(username);
        }
    }

    @RequestMapping(value="/Delete_goods",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public static String Delete_goods(HttpServletRequest req) throws SQLException, JSONException {
        {
            String username = req.getParameter("username");
            String ID = req.getParameter("ID");
            dingdan_c.dingdan_d( ID );
            return dingdan_c.dingdan_f(username);
        }
    }

    @RequestMapping(value="/rowbank_goods",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public static String rowbank_goods(HttpServletRequest req) throws SQLException, JSONException {
        {
            String username = req.getParameter("username");
            String ID = req.getParameter("ID");
            dingdan_c.dingdan_back( ID );
            return dingdan_c.dingdan_delect(username);
        }
    }

    @RequestMapping(value="/get_ware",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public static String get_ware(HttpServletRequest req) throws SQLException, JSONException {
        {
            String username = req.getParameter("username");
            return ware_c.ware_get(username);
        }
    }

    @RequestMapping(value="/get_sware",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public static String get_sware(HttpServletRequest req) throws SQLException, JSONException {
        {
            String username = req.getParameter("username");
            return ware_c.sware_get(username);
        }
    }

    @RequestMapping(value="/get_dware",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public static String get_dware(HttpServletRequest req) throws SQLException, JSONException {
        {
            String username = req.getParameter("username");
            return ware_c.dware_get(username);
        }
    }

    @RequestMapping(value="/Delete_ware",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public static String Delete_ware(HttpServletRequest req) throws SQLException, JSONException {
        {
            String username = req.getParameter("username");
            String ID = req.getParameter("ID");
            ware_c.ware_d( ID );
            return ware_c.ware_get(username);
        }
    }

    @RequestMapping(value="/rowbank_ware",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public static String rowbank_ware(HttpServletRequest req) throws SQLException, JSONException {
        {
            String username = req.getParameter("username");
            String ID = req.getParameter("ID");
            ware_c.ware_back( ID );
            return ware_c.dware_get(username);
        }
    }

    @RequestMapping(value="/ware_change",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public static String ware_change(HttpServletRequest req) throws SQLException, JSONException {
        {
            String username = req.getParameter("username");
            String ID = req.getParameter("ID");
            String biaoti = req.getParameter("biaoti");
            String pirce = req.getParameter("price");
            String number = req.getParameter("number");
            ware_c.ware_change( ID , biaoti , pirce , number );
            return ware_c.ware_get(username);
        }
    }

    @RequestMapping(value="/page_total",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public static String page_total() throws SQLException, JSONException {
        {
            return goods_c.page_t();
        }
    }

    @RequestMapping(value="/page_total1",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public static String page_total1() throws SQLException, JSONException {
        {
            return goods_c.page_t1();
        }
    }

    @RequestMapping(value="/page_total2",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public static String page_total2() throws SQLException, JSONException {
        {
            return goods_c.page_t2();
        }
    }

    @RequestMapping(value="/page_total3",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public static String page_total3() throws SQLException, JSONException {
        {
            return goods_c.page_t3();
        }
    }

    @RequestMapping(value="/goods_get",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public static String goods_get(HttpServletRequest req) throws SQLException, JSONException {
        {
            String page = req.getParameter("page");
            return goods_c.first(page);
        }
    }

    @RequestMapping(value="/veg_get",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public static String veg_get(HttpServletRequest req) throws SQLException, JSONException {
        {
            String page = req.getParameter("page");
            return goods_c.first1(page);
        }
    }

    @RequestMapping(value="/fur_get",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public static String fur_get(HttpServletRequest req) throws SQLException, JSONException {
        {
            String page = req.getParameter("page");
            return goods_c.first2(page);
        }
    }

    @RequestMapping(value="/meal_get",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public static String meal_get(HttpServletRequest req) throws SQLException, JSONException {
        {
            String page = req.getParameter("page");
            return goods_c.first3(page);
        }
    }

    @RequestMapping(value="/insert_in",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public static String insert_in(HttpServletRequest req) {
        {
            String number = req.getParameter("number[value]");
            String username = req.getParameter("username");
            String goodsid = req.getParameter("id");
            return goods_c.insert(number,username,goodsid);
        }
    }

    @RequestMapping(value="/pic_get",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String addImg(@RequestBody MultipartFile file, HttpServletRequest req, HttpServletResponse response)throws Exception{
        String biaoti = new String(req.getParameter("biaoti").getBytes("iso-8859-1"), "utf-8");
        String price = req.getParameter("price");
        String number = req.getParameter("number");
        String value = req.getParameter("value");
        String username = req.getParameter("username");

        String shuzhi = null;
        System.out.println(biaoti);
        Connection conn = connect.getConnection();
        Statement stmt = conn.createStatement();
        String sql = "select top 1 Mid (goodsid,2,5) as hao from goods where Mid (goodsid,1,1) = '"+value+"' order by ID  desc";
        ResultSet p = stmt.executeQuery(sql);
        while ( p.next() ){
            shuzhi = p.getString("hao");
        }
        System.out.println(Integer.parseInt(shuzhi)+1);
        String str = String.format("%04d", Integer.parseInt(shuzhi)+1);
        str = value + str;

        System.out.println(str);

        System.out.println("上传图片是否为空："+file.isEmpty());
        if(file != null){
            String path =null;// 文件路径
            String imgType=null;//图片类型
            String  fileName = file.getOriginalFilename();// 原文件名称
            // 判断图片类型
            imgType=fileName.indexOf(".")!=-1?fileName.substring(fileName.lastIndexOf(".")+1, fileName.length()):null;
            if(imgType!=null){
                if("GIF".equals(imgType.toUpperCase()) || "PNG".equals(imgType.toUpperCase()) || "JPG".equals(imgType.toUpperCase())){
                    // 项目在容器中实际发布运行的根路径
                    /* String realPath = request.getSession().getServletContext().getRealPath("/");*/
                    String realPath = "E:\\idea开发\\maicai\\src\\main\\webapp\\WEB-INF\\statics\\image\\";
                    // 自定义的文件名称
                    String trueFileName= str+".png";
                    // 设置图片存放的路径
                    path=realPath+trueFileName;
                    System.out.println("图片的存放路径为"+path);
                    // 转存文件到指定路径
                    file.transferTo(new File(path)); // 转存而不是写出
                    System.out.println("文件成功上传到指定目录下");

                    String sql1="insert into goods ( goodsid , pic , biaoti , price , [number] , saleman , state ) values('"+str+"','"+"/image/"+trueFileName+"','"+biaoti+"','"+price+"' , '"+number+"' , '"+username+"' , '1')";
                    Statement stmt1=conn.createStatement();//创建一个Statement对象
                    stmt1.executeUpdate(sql1);//执行sql语句
                    return "1";
                }else{
                    System.out.println("请上传GIF、PNG或者JPG格式的文件");
                }
            }else{
                System.out.println("文件类型为空");
            }
            return "2";
        }else{
            System.out.println("没有找到相对应的文件");
        }
        System.out.println("文件上传的原名称为:"+file.getOriginalFilename());

        return "";
    }

}
